<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/3/003
 * Time: 9:47
 */

namespace App\Http\Controllers\Login;
use App\Http\Controllers\Controller;
use App\Http\Controllers\PublicController;

use Illuminate\Http\Request;
use App\Common\ErrorDefine;
use Crypt;
use App\Model\Fg_admin;
class LoginController extends Controller
{

    private $admin;
    private $public;

    public function __construct(PublicController $public)
    {
        $this->admin = new Fg_admin();
        $this->public = $public;
    }

    /**
     * @param Request $request
     * 登录
     */
    public function doLogin(Request $request){

        if(empty($request->all()))
        {
            self::ShowError(ErrorDefine::INVALID_PARAMS);
            return;
        }

        //  用户名
        if(empty($request->input('username')) || !$request->has('username'))
        {
            self::ShowError(ErrorDefine::INVALID_PARAMS);
            return;
        }
        $username = $request->input('username');

        if(empty($request->input('password')) || !$request->has('password'))
        {
            self::ShowError(ErrorDefine::INVALID_PARAMS);
            return;
        }
        $password = $request->input('password');

        $admin = $this->public->checkAdminExist($username);


        if($admin == NULL)
        {
            self::ShowError(ErrorDefine::NAME_OR_PWD_ERROR);
            return;
        }

        if($password != Crypt::decrypt($admin->password))
        {
            self::ShowError(ErrorDefine::NAME_OR_PWD_ERROR);
            return;
        }

        $admin->token = Crypt::encrypt($admin->id);
        $admin->save();

        $params = [
            'username' => $admin->username,
            'token' => $admin->token,
        ];

        return self::ajaxReturn(0,'success',$params);
    }





}